package com.dld.coupon.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dld.android.net.Param;
import com.dld.coupon.util.MapUtil;
import com.dld.coupon.util.StringUtils;
import com.dld.coupon.view.AllLinearLayout;
import com.dld.coupon.view.BankLinearLayout;
import com.dld.coupon.view.CardDialogue;
import com.dld.coupon.view.GroupLinearLayout;
import com.dld.coupon.view.ListTab;
import com.dld.coupon.view.RecommendLinearLayout;
import com.dld.coupon.view.StoreLinearLayout;
import com.dld.protocol.BankCouponProtocolHelper;
import com.dld.protocol.ProtocolHelper;
import com.dld.protocol.json.Protocol;

public class SearchResultActivity extends BaseActivity {
	LinearLayout layout;

	private void initContent() {
		Intent localIntent = getIntent();
		final String keyword = localIntent.getStringExtra("keyword");
		final int type = localIntent.getIntExtra("type", 0);
		String address = localIntent.getStringExtra("address");
		boolean bool = localIntent.getBooleanExtra("is_district", false);
		int cat = localIntent.getIntExtra("cat", 255);
		if ((StringUtils.isEmpty(address)) && (!bool)
				&& (MapUtil.getCity().equals(MapUtil.cityOfRange))) {
			address = MapUtil.range;
			bool = false;
		}
		Object localObject;
		Log.v("", "*********************** search type " + type + " keyword  "
				+ keyword);
		switch (type) {
		case TYPE_SHOW_DEFAULTCONTENT:
			localObject = (AllLinearLayout) findViewById(R.id.allcontent);
			((AllLinearLayout) localObject).setVisibility(View.VISIBLE);
			((AllLinearLayout) localObject).show(
					new AllLinearLayout.ProtocolCreator() {
						public Protocol createProtocol(Param param) {
							return new Protocol(param);
						}

					}, address, bool, cat, "" + 1, keyword); // search type 1
																// for coupon
			// default
			this.layout = ((LinearLayout) localObject);
			break;
		case TYPE_SHOW_RECOMMENDCONTENT:
			localObject = (RecommendLinearLayout) findViewById(R.id.recommendcontent);
			((RecommendLinearLayout) localObject).setVisibility(0);
			((RecommendLinearLayout) localObject).show(
					new RecommendLinearLayout.ProtocolCreator() {
						public Protocol createProtocol(Context cnt, Param param) {
							return ProtocolHelper.recommondCoupon(cnt, param);
						}

					}, address, bool, cat, true, keyword, 1);
			this.layout = ((LinearLayout) localObject);
			break;
		case TYPE_SHOW_GROUPCONTENT:
			localObject = (GroupLinearLayout) findViewById(R.id.groupcontent);
			((GroupLinearLayout) localObject).setVisibility(0);
			((GroupLinearLayout) localObject).show(
					new GroupLinearLayout.ProtocolCreator() {
						public Protocol createProtocol(Context paramContext,
								Param param) {
							return new Protocol(param);
						}
					}, address, bool, cat, keyword, 2);
			this.layout = ((LinearLayout) localObject);
			break;
		case TYPE_SHOW_BANKCONTENT:
			localObject = (BankLinearLayout) findViewById(R.id.bankcontent);
			((BankLinearLayout) localObject).setVisibility(0);
			((BankLinearLayout) localObject).show(
					new BankLinearLayout.ProtocolCreator() {
						public Protocol createProtocol(
								Context paramContext,
								int paramInt1,
								int paramInt2,
								int paramInt3,
								String paramString1,
								int paramInt4,
								String paramString2,
								double paramDouble1,
								double paramDouble2,
								DialogInterface.OnDismissListener paramOnDismissListener) {
							return BankCouponProtocolHelper
									.buildSearchByKeyword(
											ActivityManager.getCurrent(),
											CardDialogue
													.getBankCodes(paramOnDismissListener),
											keyword, paramInt2, paramDouble1,
											paramDouble2, paramInt4,
											paramString2);
						}
					}, address, bool, cat);
			this.layout = ((LinearLayout) localObject);
			break;
		case TYPE_SHOW_STORECONTENT:
			localObject = (StoreLinearLayout) findViewById(R.id.storecontent);
			((StoreLinearLayout) localObject).setVisibility(0);
			((StoreLinearLayout) localObject).show(
					new StoreLinearLayout.ProtocolCreator() {
						public Protocol createProtocol(Context paramContext,
								int paramInt1, int paramInt2, int paramInt3,
								String paramString1, int paramInt4,
								String paramString2, double paramDouble1,
								double paramDouble2) {
							return ProtocolHelper
									.storeSearch(paramContext, paramInt2,
											keyword, paramString2, paramInt4);
						}
					}, address, bool, cat, true);
			this.layout = ((LinearLayout) localObject);
		}
	}

	private void initTitle() {
		setContentView(R.layout.search_result);
		((TextView) findViewById(R.id.title_text)).setText("搜索结果");
	}

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		initTitle();
		initContent();
		initSegment();
	}

	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		boolean i;
		if (((paramInt == 4) && ((this.layout instanceof ListTab)) && (((ListTab) this.layout)
				.onBack()))
				|| (super.onKeyDown(paramInt, paramKeyEvent)))
			i = true;
		else
			i = false;
		return i;
	}

	private static final int TYPE_SHOW_STORECONTENT = 4;
	private static final int TYPE_SHOW_BANKCONTENT = 3;
	private static final int TYPE_SHOW_GROUPCONTENT = 2;
	private static final int TYPE_SHOW_RECOMMENDCONTENT = 1;
	private static final int TYPE_SHOW_DEFAULTCONTENT = 0;

}
